package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.PaProductAreaDto;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.service.CommonService;
import cn.chinaflame.pagodas.service.PaProductAreaService;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 商品区域接口
 *
 * @author created by boming
 * @date 2018年1月5日 上午9:45:37
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/product/area/")
public class OpenPaProductAreaAPI {

    private static final Logger log = LoggerFactory.getLogger(OpenPaProductAreaAPI.class);

    @Autowired
    private PaProductAreaService ywProductAreaService;
    @Autowired
    private CommonService commonService;

    /**
     * 查询商品区域列表
     *
     * @param pageIndex 页码
     * @param pageSize  页数
     * @param id        区域id
     * @param name      名称
     * @return
     */
    @GetMapping("query/list")
    @ApiDoc(Result.class)
    public Object queryList(Integer pageIndex, Integer pageSize, String id, String name) {
        log.info("查询商品区域列表pageIndex:{},pageSize:{},id:{},name:{}", pageIndex, pageSize, id, name);
        if (commonService.paginationSupport(pageIndex, pageSize))
            return ywProductAreaService.queryListByPage(pageIndex, pageSize, id, name);
        return ywProductAreaService.queryList(pageIndex, pageSize, id, name);
    }

    /**
     * 查询商品区域
     *
     * @param id 区域id
     * @return
     */
    @GetMapping("query/{id}")
    @ApiDoc(Result.class)
    public Object query(@PathVariable String id) {
        log.info("查询单个商品区域id:{}", id);
        ParamPreconditions.checkNotEmpty(id, CodeEnum.FAIL.getCode(), "id不能为空");
        Result<List<PaProductAreaDto>> result = ywProductAreaService.queryList(null, null, id, null);
        List<PaProductAreaDto> dtoList = result.getData();
        ParamPreconditions.checkArgument(dtoList != null && dtoList.size() > 0 && dtoList.get(0) != null, CodeEnum.FAIL.getCode(), "未找到商品区域信息");
        return Result.create(dtoList.get(0));
    }

    /**
     * 添加单个商品区域
     *
     * @param name 区域名称
     * @return
     */
    @PostMapping(value = "add")
    @ApiDoc(Result.class)
    public Object add(String name) {
        log.info("添加单个商品区域name:{}", name);
        PaProductAreaDto dto = ywProductAreaService.add(name);
        return Result.create(dto);
    }

    /**
     * 更新单个商品区域
     *
     * @param id   区域id
     * @param name 区域
     * @return
     */
    @PutMapping("update")
    @ApiDoc(Result.class)
    public Object update(String id, String name) {
        log.info("更新单个商品区域id:{},name:{}", id, name);
        PaProductAreaDto dto = ywProductAreaService.update(id, name);
        return Result.create(dto);
    }

    /**
     * 删除商品区域信息
     *
     * @param id id
     * @return
     */
    @DeleteMapping("delete/{id}")
    @ApiDoc(Result.class)
    public Object delete(@PathVariable("id") String id) {
        log.info("删除单个商品区域id:{}", id);
        Boolean b = ywProductAreaService.delete(id);
        return Result.create(b);
    }
}
